package com.hmdp.controller;

import com.hmdp.limiter.annotation.RateLimiter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/coupon")
public class CouponController {

    /**
     * 全局秒杀接口限流
     * 10s 内最多访问100次
     */
    @GetMapping("/seckill")
    @RateLimiter(
            key = "coupon:seckill:global",
            window = 10,
            limit = 100,
            message = "当前访问人数过多，请稍后再试",
            type = RateLimiter.LimitType.METHOD
    )
    public String seckillCoupon() {
        // 模拟秒杀逻辑
        return "秒杀成功";
    }

    /**
     * 按用户限流的优惠券领取接口
     * 每个用户60s内最多领取3张优惠券
     */
    @GetMapping("/claim")
    @RateLimiter(
            key = "coupon:claim:",
            window = 60,
            limit = 3,
            message = "当前访问人数过多，请稍后再试",
            type = RateLimiter.LimitType.USER
    )
    public String claimCoupon() {
        // 模拟优惠券领取逻辑
        return "领取成功";
    }

    /**
     * 按IP限流的优惠券领取接口
     * 每个IP每秒最多查询5次商家信息
     */
    @GetMapping("/merchant")
    @RateLimiter(
            key = "coupon:merchant:",
            window = 1,
            limit = 5,
            message = "当前访问人数过多，请稍后再试",
            type = RateLimiter.LimitType.IP
    )
    public String getMerchant() {
        // 模拟商家信息查询逻辑
        return "商家信息";
    }
}
